#!/usr/bin/env python
#-*- coding:utf-8 -*-
'''
mongo 配置模型
author:cold night
email:wh_linux@126.com
'''

from model.get_db import get_db
from model.common import not_empty

__all__ = ['insert_option', 'get_option']
db = get_db()
OPTION='options'


def insert_option(option_dict):
    '''
    根据配置字典,将设置插入数据库
    '''
    not_empty(option_dict)
    try:
        for key, value in option_dict.items():
            db[OPTION].update({key:{"$exists":True}}, {"$set":{key:value}}, upsert=True)
        return True
    except:
        return False

def get_option(key):
    '''
    获取配置
    '''
    not_empty(key)
    r = db[OPTION].find_one({key:{"$exists":True}})    
    return r.get(key)
